package controller

import (
	"github.com/kataras/iris/v12"
	"go-movies/constants"
	"go-movies/model"
	"go-movies/util"
	"strconv"
)

/**
 * RegisterUser: 注册用户
 */
func RegisterUser(ctx iris.Context){
	// 读取用户上传json数据:
	data := &struct{
		Username string `json:"username"`
		Password string `json:"password"`
		Email string `json:"email"`
	}{}
	ctx.ReadJSON(data)
	// 简要判断数据是否合理:
	if data.Username == "" || data.Password == "" || data.Email == "" || !constants.EmailReg.MatchString(data.Email) {
		model.HandleErrorModel(ctx, 406, "Registration error: Check the data for errors.")
		return
	}
	// 检测用户名或者邮箱是否被注册:
	var user model.User
	user.Password = util.MD5(data.Password)
	user.Username = data.Username
	user.Email = data.Email
	if model.CheckUserInfoValid(&user) {
		if model.AddUser(&user).Error == nil {
			// 发送激活邮件:
			if model.SelectOneUser(&user, user.Username).Error == nil {
				if !util.SendMail(user.Email, user.Username, user.ID) {
					model.DeleteOneUser(&user)
					model.HandleErrorModel(ctx, 500, "Failed to send email verification code, please try again")
					return
				}
			}
			model.HandleSuccessModel(ctx, 200, nil, "Registration tip: The registered user is successful, please go to the mailbox to activate.")
			return
		}else{
			model.HandleErrorModel(ctx, 500, "Registration error: Failed to register user")
			return
		}
	}else{
		model.HandleErrorModel(ctx, 406, "The registered user name or email address has been registered, please change it")
	}
}

/**
 * 用户登录:
 */
func UserLogin(ctx iris.Context){
	// 读取用户登录信息:
	data := &struct{
		Username string `json:"username"`
		Password string `json:"Password"`
	}{}
	ctx.ReadJSON(&data)
	// 读取用户判断密码是否正确:
	var user model.User
	if model.SelectOneUser(&user, data.Username).Error == nil {
		if util.MD5(data.Password) == user.Password && user.Status != 0 {
			// 登录成功逻辑
			// 将生成token返回:
			data := &struct{
				Token string `json:"token"`
			}{
				Token:util.GetToken(user.Username, user.Email),
			}
			model.HandleSuccessModel(ctx, 200, data, "System prompt: Login successful.")
			return
		}else{
			// 密码错误:
			model.HandleErrorModel(ctx, 403, "System prompt: Login username or password error.")
			return
		}
	}else{
		// 查询错误:
		model.HandleErrorModel(ctx, 500, "System prompt: There is an error logging in")
	}
}

/**
 * UserActived: 用户激活
 */
func UserActived(ctx iris.Context){
	id, err := strconv.Atoi(ctx.Params().Get("id"))
	if err != nil {
		ctx.Text("Error: Failed to activate the user. Please check if passing the \"ID\" is correct")
		return
	}
	user := model.User{}
	user.ID = id
	if model.ActivedUser(&user) {
		ctx.Text("System prompt: Activate the user successfully! You can go and log in now.")
		return
	}else{
		ctx.Text("System prompt: Activation of user failed.")
		return
	}
}


/**
 * GetUserInfo: 获取用户信息
 */
func GetUserInfo(ctx iris.Context){
	// 获取当前用户的用户名
	username := util.GetJWTValue(ctx, "username")
	if username != ""{
		var user model.User
		if model.GetUserInfo(&user, username).Error == nil {
			user.Password = ""
			model.HandleSuccessModel(ctx, 200, user, "System prompt: obtain user information successfully")
		}else{
			model.HandleErrorModel(ctx, 500, "System prompt: Failed to obtain user information")
		}
	}else{
		model.HandleErrorModel(ctx, 500, "System tip: The server resolves the JWT authentication Token error")
	}
}